// pages/file/file.js
const app = getApp()
var util = require('../../utils/util.js')

Page({

  /**
   * 页面的初始数据
   */
  data: {
    key1:0,
    key2:0,
    key:0,
    queryValue: '',
    form_data:{type:1,public_id:0,year:'',title:'',token:wx.getStorageSync('token')},
    yearList: [],
    year: '',
    isSelect: false,
    commonValue: '公共文档列表',
    personalValue: '个人文档列表',
    fileIndex: 0,
    commonindex: -1,
    personalindex: -1,
    public_id:0,
    commonfileList: [],
    personalfileList: [],
    token:'',
    list: [
    
  ],

  },
  onSelect() {
    this.setData({
      isSelect: !this.data.isSelect
    })
  },
  changeYear(e) {
    let index = e.currentTarget.dataset.index;
    var yea = this.data.yearList[index]
    var form_year
    console.log(yea)
    if(yea == '全部'){
      form_year =0
    }else{
      form_year = yea
    }
    this.setData({
      year: yea,
      isSelect: false,
      'form_data.year':form_year
    })
    this.publicList()
  },category(e){
console.log(e)
  },
  //切换文档类型
  bindCommonChange(e){
    var that=this
    var key =  e.detail.value
    var id = that.data.data.public[key].id
    
    if(key == 0){
      //this.setData({[data.public[0]title]:'afsdf'})
    }
    this.setData({
      commonindex: e.detail.value,
      personalindex: -1,
      fileIndex: 0,
      key1:key,
      key2:0,
      'form_data.type':1,
      ['form_data.public_id']:id
    })
    this.publicList()
  },
  bindPersonalChange(e){
    util.check_login()
    var key =  e.detail.value
    var id = this.data.data.private[key].id
    this.setData({
      personalindex: e.detail.value,
      commonindex: -1,
      fileIndex: 1,
      key1:0,
      key2:key,
      'form_data.type':2,
      'form_data.title':'',
      ['form_data.public_id']:id
    })
    this.publicList()
  },

  //查询输入监听
  queryInput(e) {
    var title =  util.bindinput(e,this)
    console.log(title)
    let value = e.detail.value;
    this.setData({
      queryValue: value
    })
  },
  //查询
  onQuery() {
    let queryValue = this.data.queryValue;
    this.publicList()
    if (queryValue == '') {
      // wx.showToast({
      //   title: '请输入查询内容',
      //   icon: 'none'
      // })
    } else {
      
      //调取查询接口
    }
  },
  //查看
  goInfo(e){
    var id =e.currentTarget.dataset.id
    var key =e.currentTarget.dataset.key
    wx.navigateTo({
      url: 'detail?id=' + id+'&key='+key,
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
   
 
    var token =util.login_id()
    console.log(token)
    this.setData({'form_data.token':token})
    var token = util.login_id()
    // //  console.log(token)
    this.setData({'form_data.token':token})
      var token = getApp().globalData.token;
      //console.log(token)
      app.https_request(util.web_url.default_list,{mobile:this.data.mobile,token:this.data.token},'POST',false,false,this,getCurrentPages()).then( (res)=>{
        var len = res.data.year.length;
        //console.log(res.data.year)
          var year =util.get_field_value(res.data.year,this)
       this.setData({yearList:res.data.year,'form_data.year':0})
      }).catch( (errMsg)=>{});
      console.log(this.data.year)
      this.publicList()
  },
 
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },publicList(){
    var that = this
    this.setData({'form_data.page':1})
    wx.request({
      url: util.web_url.public_list,
      data: this.data.form_data,
      method: 'POST',
      header: {
        'content-type':  'application/x-www-form-urlencoded',
      },
      dataType: 'json',
      success: function (res) {
console.log(res.data.data.data)
that.setData({list:res.data.data.data,last_page:res.data.data.last_page})
      }
  
   })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
  
 
  },onPullDownRefresh() {
    console.log(123)
    util.msg('重新加载中','none',1000)
    this.publicList()
    setTimeout(function(){
      wx.stopPullDownRefresh();
    },500)
  },onReachBottom(){
    var that =this
    console.log("上拉加载....");
   var page = that.data.form_data.page+1
   if (that.data.last_page <= this.data.form_data.page) {
    util.msg('已全部加载完毕');
   }
   console.log(page)
   that.setData({'form_data.page':page})
    wx.request({
      url: util.web_url.public_list,
      data: that.data.form_data,
      method: 'POST',
      header: {
        'content-type':  'application/x-www-form-urlencoded',
      },
      dataType: 'json',
      success: function (res) {
// console.log(res.data.data.data)
// that.setData({list:res.data.data.data})
let list = that.data.list.concat(res.data.data.data)
that.setData({
    list: list,
    loading: false,
});
      }
  
   })
}, onShareAppMessage: function () {
 
},  onShareTimeline: function () {

}

})